﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kaffekonsumtion
{
    public static class DataBasHanterare
    {
        private const string dir = @"c:\c# 2008\kaffekonsumtion\files\";
        private const string pathProfil = dir + "profiler.txt";
        private const string pathKonsumtion = dir + "Konsumtion.bin";

       
        public static void SaveProfiler(Profil profil)
        {
            // 
            List<Profil> profiler = DataBasHanterare.GetProfil();
            StreamWriter textOut =
                new StreamWriter(
                new FileStream(pathProfil, FileMode.Append, FileAccess.Write));

            int i = 0, id = 0;
            while (i < profiler.Count())
            {
                if (profiler[i].Id > id)
                {
                    id = profiler[i].Id;
                }
                i++;

            }
            id++;


            

            textOut.Write(id + "|");
            textOut.Write(profil.Namn + "|");
            textOut.Write(profil.Avdelning + "|");
            textOut.Write(profil.Epost + "|");
            textOut.WriteLine(profil.Telefon);

            // 
            textOut.Close();
        }

        //Hämtar en profil ifrån textfilen.
        public static List<Profil> GetProfil()
        {
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            StreamReader textIn = new StreamReader(
                new FileStream(pathProfil, FileMode.Open, FileAccess.Read));

            List<Profil> profiler = new List<Profil>();

            while (textIn.Peek() != -1)
            {
                string row = textIn.ReadLine();
                string[] kolumn = row.Split('|');
                Profil profil = new Profil(kolumn[1], kolumn[2], kolumn[3], kolumn[4]);
                profil.Id = Convert.ToInt32(kolumn[0]);
                profiler.Add(profil);
            }

            textIn.Close();
            return profiler;
        }
        //public static void SparaRapport(Rapport rapport)
        //{

        //    List<Rapport> rapporter = DataBasHanterare.GetRapport();

        //    BinaryWriter binOut = new BinaryWriter(
        //        new FileStream(pathKonsumtion, FileMode.Append, FileAccess.Write));



        //    binOut.Write(rapport.Avdelning + " ");
        //    binOut.Write(rapport.Datum + " ");
        //    binOut.Write(rapport.Koppar);


        //    binOut.Close();
        //}


        //Sparar en rapport till bin-filen.
        public static void SparaRapport(Rapport rapport)
        {

            List<Rapport> rapporter = DataBasHanterare.GetRapport();

            StreamWriter textOut = new StreamWriter(
                new FileStream(pathKonsumtion, FileMode.Append, FileAccess.Write));



            textOut.Write(rapport.Avdelning + " ");
            textOut.Write(rapport.Datum + " ");
            textOut.WriteLine(rapport.Koppar);


            textOut.Close();
        

        

        //hämtar rapport ifrån bin-filen.

        //public static List<Rapport> GetRapport()
        //{
        //    if (!Directory.Exists(dir))
        //        Directory.CreateDirectory(dir);

        //    BinaryReader binIn = new BinaryReader(
        //        new FileStream(pathKonsumtion, FileMode.Open, FileAccess.Read));

        //    List<Rapport> rapporter = new List<Rapport>();

        //    while (binIn.PeekChar() != -1)
        //    {
        //        string row = binIn.ReadString();
        //        string[] kolumn = row.Split(' ');
        //        Rapport rapport = new Rapport(kolumn[0], DateTime.Parse(kolumn[1]), Convert.ToInt32(kolumn[3]));
        //        rapporter.Add(rapport);
        //    }
        //    binIn.Close();
        //    return rapporter;
        }


        public static List<Rapport> GetRapport()
        {
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            StreamReader textIn = new StreamReader(
                new FileStream(pathKonsumtion, FileMode.Open, FileAccess.Read));

            List<Rapport> rapporter = new List<Rapport>();

            while (textIn.Peek() != -1)
            {
                string row = textIn.ReadLine();
                string[] kolumn = row.Split(' ');
                Rapport rapport = new Rapport(kolumn[0], DateTime.Parse(kolumn[1]), Convert.ToInt32(kolumn[3]));
                rapporter.Add(rapport);
            }
            textIn.Close();
            return rapporter;
        }
    }
}
